<?php

class System_Models_Unit extends Zend_Db_Table {
    protected $_name = "unit";
    protected $_primary = "UnitID";
    protected $_primary_name = "UnitID";
   

    public function getOrderMax(){
        return 1;
    }
     public function getAllTree(){
         $query = "SELECT * FROM $this->_name ";
        // echo $query;die();
        try {
            $data = $this->getAdapter()->fetchAll($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $data;
    }
    public function selectRow($id,$ParentId,$ParentName){
        if($id){
            // $sql = "call getSingleUnit($id)";
            $sql = " SELECT A.UnitID, A.UnitParent, A.Code, A.Name, A.Tel, A.Fax, A.Email, A.Address, A.Orders, A.Status, A.OwnerCode, B.Name As ParentName";
            $sql .= " FROM unit A";
            $sql .= " INNER JOIN unit B ON A.UnitParent = B.UnitID";
            $sql .= " WHERE A.UnitID = $id";
            try {
                $rows = $this->getDefaultAdapter()->fetchRow($sql);
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }else{
            $query = $this->select()->from($this, '')->columns(array('Orders' => new Zend_Db_Expr('MAX(Orders)')));
            try {
                $orders = $this->getAdapter()->fetchRow($query);
            } catch (Exception $e) {
                echo $e->getMessage();
            }
            $rows = array(
							'UnitID'=>''
                            ,'Code'=>''
                            ,'Name'=>''
                            ,'Orders'=>$orders['Orders']+1
                            ,'Status'=>'1'
                            ,'UnitParent'=>$ParentId
                            ,'ParentName'=>$ParentName
                            ,'Address' => ''
                            ,'OwnerCode'=> ''
                            ,'Tel' => ''
                            ,'Email' => ''
                        );
        }
        return $rows;
    }
   
    public function getIDAllowDelete($arrList) {
        $query = "SELECT UnitID FROM unit ";
        $query .= " Where ".$this->getAdapter()->quoteInto("UnitParent IN (?)", $arrList);
        // echo $query; die;
        try {
            $result = $this->getAdapter()->fetchRow($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        if ($result) {
            $arrList = array_diff($arrList, $result);
        }

        $query = "SELECT UnitID FROM staff ";
        $query .= " Where ".$this->getAdapter()->quoteInto("UnitID IN (?)", $arrList);
        // echo $query; die;
        try {
            $result = $this->getAdapter()->fetchRow($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        if ($result) {
            $arrList = array_diff($arrList, $result);
        }
        return $arrList; 
        // $query2 = 
    }
    public function deleteRows($arrNodeID) {
        $result = array();
        try {
            $where = $this->getAdapter()->quoteInto("UnitID IN (?)", $arrNodeID);
            $result = $this->delete($where);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $result;
    }
}